<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-button type="primary" @click="showAddRole">添加角色</el-button>
                <el-table ref="roleList" border :data="roleList" highlight-current-row style="width: 100%">
                    <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-form label-position="left" inline class="demo-table-expand">
                            <el-row v-for="(item,i) in scope.row.children" :key="item.id" :class="['borderB',i== 0 ? 'borderT':'' ,'vcenter']">
                                <el-col :span="5">
                                    <el-tag closable @close="delRole(scope.row,item.id)" >
                                        {{item.authName}}
                                    </el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="18">
                                    <el-row v-for="item1 in item.children" :key="item1.id"  class="borderB vcenter">
                                        <el-col :span="5">
                                            <el-tag closable type="success" @close="delRole(scope.row,item1.id)">
                                                {{item1.authName}}
                                            </el-tag>
                                            <i class="el-icon-caret-right"></i>
                                        </el-col>
                                        <el-col :span="18">
                                            <!-- <el-row> -->
                                                <el-tag closable type="warning" v-for="item2 in item1.children" :key="item2.id" @close="delRole(scope.row,item2.id)">
                                                    {{item2.authName}}
                                                </el-tag>
                                            <!-- </el-row> -->
                                        </el-col>
                                    </el-row>
                                </el-col>
                                
                            </el-row>
                            
                        </el-form>
                    </template>
                    </el-table-column>
                    <el-table-column type="index" width="50">
                    </el-table-column>
                    <el-table-column property="roleName" label="角色名称">
                    </el-table-column>
                    <el-table-column property="roleDesc" label="角色描述">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary" icon="el-icon-edit" @click="editRole(scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" icon="el-icon-delete" @click="delRole(scope.row)">删除</el-button>
                            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showRights(scope.row)">分配权限</el-button>
                        </template>
                    </el-table-column>
            </el-table>
            <!-- 分配权限弹出框 -->
            <el-dialog title="分配权限" :visible.sync="showRightsVisible"  width="50%" :before-close="showRightsClose">
                <el-tree :data="rightsList" ref="treeRef" show-checkbox default-expand-all  highlight-current node-key="id"  :default-checked-keys="defChecked" :props="treeList">

                </el-tree>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="showRightsClose">取 消</el-button>
                    <el-button type="primary" @click="AddRightsBtn">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 添加角色弹出框 -->
            <el-dialog title="添加角色" :visible.sync="addRoleVisible" width="55%" :before-close="CloseAddrole">
                <el-form :model="roleForm" :rules="rules"  ref="roleForm" label-width="100px">
                    <el-form-item label="角色名称" prop="roleName" >
                        <el-input v-model="roleForm.roleName"></el-input>
                    </el-form-item>
                     <el-form-item label="角色描述" prop="roleDesc" >
                        <el-input v-model="roleForm.roleDesc"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="CloseAddrole">取 消</el-button>
                    <el-button type="primary" @click="addRole">确 定</el-button>
                </span>
            </el-dialog>
             <!-- 编辑角色弹出框 -->
            <el-dialog title="编辑角色" :visible.sync="editRoleVisble" width="55%" :before-close="CloseEditRole">
                <el-form :model="editForm" :rules="rules"  ref="editForm" label-width="100px">
                    <el-form-item label="角色名称" prop="roleName" >
                        <el-input v-model="editForm.roleName"></el-input>
                    </el-form-item>
                     <el-form-item label="角色描述" prop="roleDesc" >
                        <el-input v-model="editForm.roleDesc"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="CloseEditRole">取 消</el-button>
                    <el-button type="primary" @click="editRoleUpdata">确 定</el-button>
                </span>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
    export default{
        data(){
            return {
                roleList:[],
                showRightsVisible:false,
                addRoleVisible:false,
                editRoleVisble:false,
                rightsList:[],
                treeList:{
                    label:'authName',
                    children:'children'
                },
                defChecked:[],
                rightsKeys:[],
                roleId:'',
                roleForm:{
                    roleName:'',
                    roleDesc:''
                },
                editForm:{
                    roleName:'',
                    roleDesc:''
                },
                editYZForm:{
                    id:'',
                    roleName:'',
                    roleDesc:''
                },
                rules:{
                    roleName: [
                        { required: true, message: '请输入角色名称', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    roleDesc: [
                        { required: true, message: '请输入角色描述', trigger: 'blur' },
                        { min: 5, max: 16, message: '长度在 5 到 20 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        created(){
            this.getRoles()
            
        },
        methods:{
            // 获取角色信息函数
            async getRoles(){
                const {data:res} = await this.$http.get('/roles')
                if(res.meta.status !== 200) return this.$message.error('获取角色列表失败~')
                this.roleList = res.data
            },
            // 删除某个权限函数
            async delRole(data,id){
                // 弹出一个提示框确认是否要删除
                const res = await this.$confirm('此操作将删除该权限, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).catch(()=>{})
                if(res !== 'confirm') return this.$message.info('已取消删除~')
                // 确认删除后发送ajax请求
                const {data:sta} = await this.$http.delete('roles/'+data.id+'/rights/'+id)
                if(sta.meta.status !== 200) return this.$message.error('删除权限失败~')
                data.children = sta.data
            },
            // 展示分配权限的树形控件的函数
            async showRights(role){
                this.roleId = role.id
                this.defChecked = []
                const {data:res} = await this.$http.get('/rights/tree')
                if(res.meta.status !== 200)return this.$message.error('获取权限列表失败~')
                this.rightsList = res.data
                this.getRoleid(role,this.defChecked)
                this.showRightsVisible = true
            },
            // 关闭分配权限的对话框
            showRightsClose(){
                this.showRightsVisible = false
            },
            // 设置一个递归函数.获取角色的权限信息
            getRoleid(node,arr){
                
                if(!node.children){
                   return  arr.push(node.id)
                }
                node.children.forEach(item => {
                    this.getRoleid(item,arr)
                });

            },
            // 确认添加权限按钮的处理事件
            async AddRightsBtn(){
                const key = [
                    ...this.$refs.treeRef.getCheckedKeys(),
                    ...this.$refs.treeRef.getHalfCheckedKeys()
                ]
                const str = key.join(',')
                const {data:res} = await this.$http.post('/roles/'+this.roleId+'/rights',{rids:str})
                if(res.meta.status !== 200)return this.$message.error('分配权限失败,请稍后再试~')
                this.getRoles()
                this.showRightsClose()

            },
            // 添加角色处理函数 
            addRole(){
                this.$refs.roleForm.validate(async (vali)=>{
                    if(!vali)return this.$message.error('请填写正确的信息哦~')
                    const {data:res}  = await this.$http.post('/roles',this.roleForm)
                    if(res.meta.status !== 201)return this.$message.error('添加角色失败!稍后再试哦~')
                    this.$refs.roleForm.resetFields()
                    this.getRoles()
                    this.$message.success('恭喜你成功的添加了一个角色 ,干的漂亮~')
                    this.addRoleVisible = false
                    
                })
            },
            // 点击添加按钮角色按钮事件
            showAddRole(){
                this.addRoleVisible = true
            },
            // 关闭添加角色按钮处理事件
            CloseAddrole(){
                this.addRoleVisible = false
                this.$refs.roleForm.resetFields()
            },
            // 删除角色按钮处理事件
            async delRole(data){
                const isdel = await this.$confirm('次操作会删除该角色,还要继续执行嘛?','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).catch(()=>{})
                if(isdel !== 'confirm') return this.$message.info('已取消删除~')
                const {data:res} = await this.$http.delete('/roles/'+data.id)
                if(res.meta.status !== 200) return this.$message.error('删除角色失败!稍后再试~')
                this.$message.success('恭喜你成功的删除了一个角色 ,干的漂亮~')
                this.getRoles()
            },
            //编辑按钮的处理事件
            editRole(data){
                this.editRoleVisble = true
                this.editForm.roleName = data.roleName     
                this.editForm.roleDesc = data.roleDesc   
                this.editYZForm.roleName = data.roleName     
                this.editYZForm.roleDesc = data.roleDesc   
                this.editYZForm.id = data.id   
            },
            // 关闭编辑角色按钮
            CloseEditRole(){
                this.editRoleVisble = false
                this.$refs.editForm.resetFields()
            },
            //编辑角色页面确定按钮
            editRoleUpdata(){
                if(this.editYZForm.roleName == this.editForm.roleName && this.editYZForm.roleDesc == this.editForm.roleDesc){
                    return this.$message.error('先别急啊! 你啥都没改呢~')
                }
                this.$refs.editForm.validate(async (valid)=>{
                    if(!valid) return this.$message.error('请输入正确的信息哦~')
                    const {data:res} = await this.$http.put('/roles/'+this.editYZForm.id,this.editForm)
                    if(res.meta.status !== 200) return this.$message.error('更新角色信息失败!稍后再试试吧~')
                    this.$message.success('更新角色信息成功~')
                    this.getRoles()
                    this.editRoleVisble = false
                    
                })
            }
        }
    }
</script>

<style scoped>
    .el-tag {
        margin-bottom: 15px;
        margin-top: 15px;
        margin-right: 10px;
    }
    .borderB {
        border-bottom: 1px solid #eee;
    }
    .borderT {
        border-top: 1px solid #eee;
    }
    .vcenter {
        display: flex;
        align-items: center;
    }
    .el-row:last-child {
        border:none;
    }
    .el-button {
        margin-bottom: 15px;
    }
</style>